﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Console = Colorful.Console;

namespace Dapper.Moon.DbFirst
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0 || args.Length == 1)
            {
                Console.WriteAscii(" Dapper.Moon", Color.BlueViolet);
                Console.WriteFormatted(@"
{5}
> Dapper.Moon.DbFirst {0} mysql {1} server=localhost... {2} project.entity {3} t_user,t_role {4} ""
{0}     数据库，SqlServer、MySql、Oracle
{1}         数据库连接字符串
{2}  命名空间 project.entity
{3}     表名称   t_user,t_role 为空为当前数据库所有表
{4}     输出路径 如：c:\moon，默认当前所在目录
"
, Color.SlateGray,
new Colorful.Formatter("-dbtype", Color.ForestGreen),
new Colorful.Formatter("-db", Color.ForestGreen),
new Colorful.Formatter("-namespace", Color.ForestGreen),
new Colorful.Formatter("-tables", Color.ForestGreen),
new Colorful.Formatter("-output", Color.ForestGreen),
new Colorful.Formatter("# 调用示例 #", Color.YellowGreen)
);
                return;
            }

            try
            {
                DbFirstParameter option = new DbFirstParameter();

                for (int i = 0; i < args.Length; i++)
                {
                    switch (args[i].Trim().ToLower())
                    {
                        case "-dbtype":
                            string dbType = args[i + 1];
                            switch (dbType.Trim().ToLower())
                            {
                                case "mysql": option.DbType = Dapper.Moon.DbType.MySql; break;
                                case "sqlserver": option.DbType = Dapper.Moon.DbType.SqlServer; break;
                                case "oracle": option.DbType = Dapper.Moon.DbType.Oracle; break;
                                default: throw new ArgumentException($"-dbtype 参数错误");
                            }
                            break;
                        case "-db":
                            option.Db = args[i + 1];
                            break;
                        case "-namespace":
                            option.Namespace = args[i + 1];
                            break;
                        case "-tables":
                            option.Tables = args[i + 1];
                            break;
                        case "-output":
                            option.Output = args[i + 1];
                            break;
                    }
                }

                if (string.IsNullOrWhiteSpace(option.Db))
                {
                    throw new ArgumentException($"-db 参数为空");
                }

                //Console.Write("\r\n" + option.ToString());

                using (IDapperMoon dm = DbFactory.Create(option.Db, option.DbType))
                {
                    string dire = string.IsNullOrWhiteSpace(option.Output)
                        ? AppDomain.CurrentDomain.BaseDirectory + "Model" : option.Output;

                    if (!System.IO.Directory.Exists(dire))
                    {
                        System.IO.Directory.CreateDirectory(dire);
                    }

                    //Console.Write("\r\n" + option.Tables?.Split(',').Length);

                    dm.DbFirst().Builder(new DbFirstOption()
                    {
                        Namespace = string.IsNullOrWhiteSpace(option.Namespace) ? "project.entity" : option.Namespace,
                        SaveFolder = dire,
                        Tables = string.IsNullOrWhiteSpace(option.Tables) ? null : option.Tables?.Split(',')
                    });

                    Console.WriteFormatted($"\r\nsuccess", Color.YellowGreen);
                }
            }
            catch (Exception ex)
            {
                Console.WriteFormatted($"\r\nerror:{ex.Message}", Color.Red);
            }
            //Console.ReadKey();
        }
    }

    class DbFirstParameter
    {
        public Dapper.Moon.DbType DbType { get; set; }
        public string Db { get; set; }
        public string Namespace { get; set; }
        public string Tables { get; set; }
        public string Output { get; set; }

        public override string ToString()
        {
            return $"DbType={DbType},Db={Db},Namespace={Namespace},Tables={Tables},Output={Output}";
        }
    }
}
